package edu.bu.bga;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
import edu.bu.bga.generator.QueueGenerator;
import edu.bu.bga.generator.RandomIntegerQueueGenerator;
import edu.bu.bga.queue.Queue;

public class GetQueueActivity extends Activity {

	private Button backButton;
	private Button generateQueueButton;
	private TextSwitcher textSwitcher;
	private SeekBar seekBar;
	private TextView playerNumText;

	private int numPlayers;

	private QueueGenerator queueGenerator;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queuelayout);

		backButton = (Button) this.findViewById(R.id.backbutton);
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		queueGenerator = new RandomIntegerQueueGenerator();

		textSwitcher = (TextSwitcher) this.findViewById(R.id.textSwitcher1);
		textSwitcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				TextView t = new TextView(getBaseContext());
				t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
				t.setTextSize(36);
				return t;

			}
		});

		seekBar = (SeekBar) findViewById(R.id.seekBar1);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromTouch) {

				// add 2 to adjust for min
				numPlayers = progress + 2;
				updatePlayerText();
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

		});
		playerNumText = (TextView) findViewById(R.id.playerNum);

		generateQueueButton = (Button) this
				.findViewById(R.id.generatequeuebutton);
		generateQueueButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Queue queue = queueGenerator.generateQueue(numPlayers);
				StringBuilder sb = new StringBuilder();
				sb.append("\n");
				sb.append(queue.getQueueText());
				textSwitcher.setText(sb.toString());
			}
		});

	}

	protected void updatePlayerText() {
		playerNumText.setText("Player Num: " + numPlayers);

	}

}
